package com.his.his_system.controller.VisitController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.his_system.controller.VisitController.DefaultBean.Obj;
import com.his.his_system.domain.DzmHisDoctor;
import com.his.his_system.service.DzmHisDoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 待做
 */
@Component
@Slf4j
public class YxyTodo {

    @Resource
    private DzmHisDoctorService dzmHisDoctorService;


    public  Integer getHospitalid(){
        //TODO  医院id  要从redis获取或则SecurityContext获取
//        Map<String,Object> o = (Map<String,Object>)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        Integer i = Integer.valueOf(o.get("pId").toString());
//        log.error("获取的hid="+i);
        return 1;

    }

    public  Integer getDoctorid(){
        //TODO  医生id  要从redis获取或则SecurityContext获取
        QueryWrapper<DzmHisDoctor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",getUserid());
        DzmHisDoctor doctor =  dzmHisDoctorService.getOne(queryWrapper);

        Object i =  doctor.getId();
        Integer ii = Integer.valueOf(i.toString());
        log.error("获取的did="+ ii);
        return  ii;
    }



    public  Integer getUserid(){
        //TODO  操作员id//当前用户id 要从redis获取或则SecurityContext获取
        Map<String,Object> o = (Map<String,Object>)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer i =  Integer.valueOf( o.get("uid").toString());
        log.error("获取的uid="+i);
        return  i;
    }


    //TODO 定时任务处理 规定时间挂号未缴费和未就诊的信息作废


    /**
     * 获取指定时间戳（秒级）
     * @param date
     * @param i
     * @return
     */
    public static Integer getDateSeconds(String date,int i){
        String[] str=date.split(",");
        if(i==1){
            return getsu(str[0]);
        }else {
            return getsu(str[1]);
        }
    }
    public static Integer getsu(String data){
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = dateformat.parse(data);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return (int)(date.getTime()/1000);
    }

    public static void main(String[] args) {


        System.out.println(getDate());
        System.out.println(getDateNextDay());

    }


    public static java.util.Date getNextMonDate() {
        java.util.Calendar cal =  java.util.Calendar.getInstance();
        //1-7  1= Sunday
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        int disDay = 9 - dayOfWeek;
        int differenceValue = disDay > 7?(disDay/7):disDay;
        cal.add(Calendar.DATE, differenceValue);
        cal.set(Calendar.HOUR_OF_DAY,5);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        return cal.getTime();
    }



    public static String getDate(){
        Date date =YxyTodo.getNextMonDate();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(date);
    }
    public static String getDateNextDay(){
        Date date =YxyTodo.getNextMonDate();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);  // 设置当前日期
        calendar.add(Calendar.DATE, 7);  // 增加7天
        Date newDate = calendar.getTime();  // 获取新日期
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String s1=df1.format(newDate);
        return s1;
    }
}
